package com.naiterui.ehp.bs.ds.modules.goods.controller;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.ds.common.utils.JwtUser;
import com.naiterui.ehp.bs.ds.common.utils.PageVO;
import com.naiterui.ehp.bs.ds.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.ds.modules.goods.feign.WarehouseGoodsFeignClient;
import com.naiterui.ehp.bs.ds.modules.goods.service.IWarehouseGoodsService;
import com.naiterui.ehp.bs.ds.modules.goods.vo.WarehouseGoodsInventoryVO;
import com.naiterui.ehp.bs.ds.modules.goods.vo.WarehouseGoodsVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@Api(tags = {SwaggerTag.DS + SwaggerTag.PHARMACY})
@RestController
@RequestMapping("pharmacy/warehouse/goods")
public class WarehouseGoodsController {

  @Autowired
  private IWarehouseGoodsService warehouseGoodsService;
  @Autowired
  private WarehouseGoodsFeignClient warehouseGoodsFeignClient;

  /**
   * 查询合作商品
   *
   * @return
   */
  @SysLog("查询合作商品")
  @GetMapping("/list")
  @ApiOperation("查询合作商品")
  @PreAuthorize("@p.check('warehouse:goods:list')")
  public PageVO<WarehouseGoodsVO> queryPage(PageParamsVO pageParamsVO, WarehouseGoodsVO warehouseGoods) {
    log.info("查询合作商品 请求参数：pageParamsVO = {} warehouseGoods = {}", pageParamsVO, warehouseGoods);
    return this.warehouseGoodsService.queryPage(pageParamsVO, warehouseGoods);
  }

  /**
   * 已选合作商品
   *
   * @return
   */
  @SysLog("已选合作商品")
  @GetMapping("/select/list")
  @ApiOperation("已选合作商品")
  @PreAuthorize("@p.check('warehouse:goods:selectList')")
  public PageVO<WarehouseGoodsVO> selectList(PageParamsVO pageParamsVO) {
    return this.warehouseGoodsService.queryPage(pageParamsVO, new WarehouseGoodsVO());
  }

  @PostMapping
  @SysLog("修改库存")
  @ApiOperation("修改库存")
  @PreAuthorize("@p.check('warehouse:goods:inventory')")
  public void updateInventory(@RequestBody Set<WarehouseGoodsInventoryVO> warehouseGoods) throws BusinessException {
    log.info("修改库存 请求参数：warehouseGoods = {}", warehouseGoods);
    JwtUser currentUser = SecurityUtils.getCurrentUser();
    Long warehouseId = currentUser.getWarehouseId();
    this.warehouseGoodsFeignClient.updateInventory(warehouseId, warehouseGoods);
  }

  @PostMapping("/bacth/update")
  @SysLog("批量修改库存")
  @ApiOperation("批量修改库存")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "file", value = "文件", paramType = "form",
          dataType = "file")
  })
  @PreAuthorize("@p.check('warehouse:goods:inventory')")
  public void updateInventoryFile(MultipartFile file) throws BusinessException {
    log.info("批量修改库存");
    this.warehouseGoodsService.updateInventory(file);
  }

}
